<?php 

// Desired folder structure
$structure = '../g-konten/fm/template/'.$_GET['idmod'].'/thumb/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.
if (!file_exists($structure)){
if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}
}
// ...

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1") && ($_FILES["foto"]["type"]=='image/jpeg')) {
mysql_select_db($database_konek, $konek);
$query_t = "SELECT * FROM template_data";
$t = mysql_query($query_t, $konek) or die(mysql_error());
$row_t = mysql_fetch_assoc($t);
$totalRows_t = mysql_num_rows($t);
$gb=$totalRows_t+1;
  $insertSQL = sprintf("INSERT INTO template_data (id_module, image, nama, link, harga) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_GET['idmod'], "int"),
                       GetSQLValueString($image=md5($gb).".jpg", "text"),
                       GetSQLValueString($_POST['nama'], "text"),
                       GetSQLValueString($_POST['link'], "text"),
                       GetSQLValueString($_POST['harga'], "int"));

   if (file_exists('../g-konten/fm/template/'.$_GET['idmod'].'/' . $image))
      {
        echo $_FILES["foto"]["name"] . " already exists. ";
      }
    else
      {
       move_uploaded_file($_FILES["foto"]["tmp_name"],
      '../g-konten/fm/template/'.$_GET['idmod'].'/' . $image);
      } 

mysql_select_db($database_konek, $konek);
$query_plugtext = "SELECT * FROM template WHERE id = '$_GET[idmod]' ORDER BY id ASC";
$plugtext = mysql_query($query_plugtext, $konek) or die(mysql_error());
$row_plugtext = mysql_fetch_assoc($plugtext);
$totalRows_plugtext = mysql_num_rows($plugtext);

// *** Include the class
	include("resize-class.php");

	// *** 1) Initialise / load image
	$resizeObj = new resize('../g-konten/fm/template/'.$_GET['idmod'].'/' . $image);

	// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
	$resizeObj -> resizeImage($row_plugtext['width'], $row_plugtext['height'], 'crop');

	// *** 3) Save image
	$resizeObj -> saveImage('../g-konten/fm/template/'.$_GET['idmod'].'/thumb/' . $image, 100);
	
mysql_free_result($plugtext);


  mysql_select_db($database_konek, $konek);
  $Result1 = mysql_query($insertSQL, $konek) or die(mysql_error());

  $insertGoTo = "home.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
   ?>
	
	 <script language="javascript">
  	<!--
     	    alert('Input Success')
			window.location = "<?php echo sprintf("%s", $insertGoTo) ?>";
			
   	--></script>
	<?php
} else
{
	if($_FILES["foto"])
   echo "Not a JPEG file.Please choose one JPEG file to upload";
}



mysql_select_db($database_konek, $konek);
$query_plugtext = "SELECT * FROM template_data WHERE id_module = '$_GET[idmod]' ORDER BY id ASC";
$plugtext = mysql_query($query_plugtext, $konek) or die(mysql_error());
$row_plugtext = mysql_fetch_assoc($plugtext);
$totalRows_plugtext = mysql_num_rows($plugtext);
?>
<a href="home.php?page=pluginview&p=<?php echo $_GET['p'] ?>&root=add_module.php">&lt;&lt;back</a>
<?php if ($totalRows_plugtext > 0) { // Show if recordset not empty ?>
  <table  id="example-datatable" class="table table-bordered table-hover dataTable" aria-describedby="example-datatable_info" >
    <tr>
      <td width="50">id</td>
      <td>image</td>
      <td>nama</td>
      <td width="50">Actions</td>
    </tr>
    <?php do { ?>
      <tr>
        <td><?php echo $row_plugtext['id']; ?></td>
        <td><img src="../g-konten/fm/template/<?php echo $_GET['idmod'] ?>/thumb/<?php echo $row_plugtext['image']; ?>" /></td>
        <td><?php echo $row_plugtext['nama']; ?></td>
        <td><div class="btn-group">
          <a href="home.php?page=pluginview&p=<?php echo $_GET['p'] ?>&root=edit_data_module.php&id=<?php echo $row_plugtext['id']; ?>&idmod=<?php echo $_GET['idmod'] ?>" data-toggle="tooltip" title="" class="btn btn-xs btn-default" data-original-title="Edit"><i class="fa fa-pencil"></i></a>
          <a href="home.php?page=pluginview&p=<?php echo $_GET['p'] ?>&root=delete_data_module.php&id=<?php echo $row_plugtext['id']; ?>&idmod=<?php echo $_GET['idmod'] ?>" data-toggle="tooltip" title="" class="btn btn-xs btn-default" data-original-title="Delete"><i class="fa fa-times"></i></a>
</div></td>
      </tr>
      <?php } while ($row_plugtext = mysql_fetch_assoc($plugtext)); ?>
  </table>
  <?php } // Show if recordset not empty ?>
  <p>&nbsp;</p>
  <form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <table width="100%" align="center">
      <tr valign="baseline">
        <td width="12%" align="left" valign="middle" nowrap="nowrap"><strong>Judul :</strong></td>
        <td width="88%"><input type="text" name="nama" class="form-control" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td width="12%" align="left" valign="middle" nowrap="nowrap"><strong>link :</strong></td>
        <td width="88%"><input type="text" name="link" class="form-control" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td width="12%" align="left" valign="middle" nowrap="nowrap"><strong>harga :</strong></td>
        <td width="88%"><input type="text" name="harga" class="form-control" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td align="left" valign="middle" nowrap="nowrap"><strong>image :</strong></td>
        <td valign="top"><label for="image"></label>
        <input type="file" name="foto" id="foto" /></td>
      </tr>
      <tr valign="baseline">
        <td align="left" valign="top" nowrap="nowrap">&nbsp;</td>
        <td><input type="submit" value="Submit" id="status-update-btn" class="btn btn-primary pull-right" /></td>
      </tr>
    </table>
    <input type="hidden" name="id" value="" />
    <input type="hidden" name="MM_insert" value="form1" />
</form>
 
<?php

mysql_free_result($plugtext);
?>
